package hos.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.Utils
import hos.demo.ui.theme.FileXTheme
import hos.filex.FileX
import hos.permission.PermissionX
import kotlin.concurrent.thread

class MainActivity : FragmentActivity() {

    private val permissionX = PermissionX.init(this)
    private val fileX = FileX.file(this)
    private val fileOpen = FileX.open(this)


    private var imagePath by mutableStateOf("")
    private var imagePathList by mutableStateOf<MutableList<String>>(mutableListOf())
    private var locationEnabled by mutableStateOf(false)

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Utils.init(application)
        FileX.setAuthority(this, "$packageName.utilcode.fileprovider")
        setContent {
            FileXTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    LazyColumn(content = {
                        item {
                            ListItem(headlineText = {
                                Text(text = "拍照")
                            }, modifier = Modifier.clickable {
                                // 模拟器不行
                                permissionX.requestCamera {
                                    // 拍照
                                    fileX.createCameraImage {
                                        if (it == null) {
                                            // 拍照失败
                                            imagePath = ""
                                            Log.d("RESULT", "createCameraImage: null")
                                            return@createCameraImage
                                        }
                                        // 拍照成功
                                        Log.d("RESULT", "createCameraImage: $imagePath")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "录像")
                            }, modifier = Modifier.clickable {
                                // 模拟器不行
                                permissionX.requestAllStorage {
                                    // 录像
                                    fileX.createCameraVideo {
                                        if (it == null) {
                                            // 录像失败
                                            imagePath = ""
                                            Log.d("RESULT", "createCameraVideo: null")
                                            return@createCameraVideo
                                        }
                                        // 录像成功
                                        imagePath = it
                                        Log.d("RESULT", "createCameraVideo: $imagePath")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "录音")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    // 录音
                                    fileX.createCameraAudio {
                                        if (it == null) {
                                            // 录音失败
                                            imagePath = ""
                                            Log.d("RESULT", "createCameraAudio: null")
                                            return@createCameraAudio
                                        }
                                        // 录音成功
                                        imagePath = it
                                        Log.d("RESULT", "createCameraAudio: $imagePath")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    // 选择图片
                                    fileX.chooseImage {
                                        if (it == null) {
                                            // 选择图片失败
                                            imagePath = ""
                                            Log.d("RESULT", "chooseImage: null")
                                            return@chooseImage
                                        }
                                        // 选择图片成功
                                        imagePath = it
                                        Log.d("RESULT", "chooseImage: $imagePath")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片和视频")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    fileX.chooseImageVideo {
                                        if (it == null) {
                                            imagePath = ""
                                            Log.d("RESULT", "chooseImageVideo: null")
                                            return@chooseImageVideo
                                        }
                                        imagePath = it
                                        Log.d("RESULT", "chooseImageVideo: $imagePath")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片,多选")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    // 选择多个图片
                                    fileX.chooseImageList {
                                        if (it == null) {
                                            // 选择多个图片失败
                                            imagePathList = mutableListOf()
                                            Log.d("RESULT", "chooseImageList: null")
                                            return@chooseImageList
                                        }
                                        // 选择多个图片成功
                                        imagePathList = it
                                        Log.d("RESULT", "chooseImageList: $imagePathList")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "选择图片和视频,多选")
                            }, modifier = Modifier.clickable {
                                permissionX.requestAllStorage {
                                    fileX.chooseImageVideoDocumentList {
                                        if (it == null) {
                                            imagePathList = mutableListOf()
                                            Log.d("RESULT", "chooseImageVideoDocumentList: null")
                                            return@chooseImageVideoDocumentList
                                        }
                                        imagePathList = it
                                        Log.d(
                                            "RESULT",
                                            "chooseImageVideoDocumentList: $imagePathList"
                                        )
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "打开定位设置页面 $locationEnabled")
                            }, modifier = Modifier.clickable {
                                // 申请定位权限
                                permissionX.requestLocation {
                                    // 检查是否已经打开系统定位
                                    fileOpen.checkLocationSetting {
                                        locationEnabled = it
                                        Log.d("RESULT", "locationEnabled $it")
                                    }
                                }
                            })
                            ListItem(headlineText = {
                                Text(text = "imagePath $imagePath")
                            })
                            ListItem(headlineText = {
                                Text(text = "imagePathList $imagePathList")
                            })
                        }
                    })
                }
            }
        }
    }

}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    FileXTheme {
        Greeting("Android")
    }
}